//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		$('#materialscrapDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#materialscrapDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_cate:1000,
				goods_name:goods_name,
				bar_code:bar_code
			}
		});
	})
	//更新行数据
	function updateActions(value){
		$.post(confirmEditmaterialscrapUrl,{id:value.id,scrap_price:value.scrap_price,material_price:value.material_price},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#materialscrapDatagrid').datagrid('validateRow', editIndex)){
			$('#materialscrapDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#materialscrapDatagrid').datagrid('selectRow', index);
				$('#materialscrapDatagrid').datagrid('endEdit', editIndex);
				$('#materialscrapDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#materialscrapDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};

	$('#materialscrapDatagrid').datagrid({
		//url:getmaterialscrapUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getgoodslistUrl,
		title:'商品管理',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		queryParams:{
			goods_cate:1000,
			page:1
		},
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'manua_id',title:'账册编号',sortable:true},
		{field:'goods_name',title:'商品名称'},
		{field:'material_price',title:'损耗单价',
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空'
					}  
				}
		},
		{field:'scrap_price',title:'边角料单价',
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空'
					}  
				}
		},
		{field:'goods_spec',title:'规格'},
		{field:'bar_code',title:'条形码'},
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
materialscrapObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#materialscrapDatagrid').datagrid('validateRow', editIndex)){
			$('#materialscrapDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#materialscrapDatagrid').datagrid('acceptChanges');
		}
	},
	editBar:function(){
		var materialscrapRow=$('#materialscrapDatagrid').datagrid('getSelections');
        if (materialscrapRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(materialscrapRow.length==1){            	
        	$('#materialscrapEditDialog').dialog('open').dialog('setTitle','编辑商品');
            $('#materialscrapEditForm').form('load',materialscrapRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	/* infoBar:function(){//查看商品详细
		var materialscrapRow=$('#materialscrapDatagrid').datagrid('getSelections');
        if (materialscrapRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(materialscrapRow.length==1){
			// console.log(materialscrapRow[0]['id']);
			var id=materialscrapRow[0]['id'];
			var inbound_status=1;
        	showTab(materialscrapInfoUrl,'确认到货',id,inbound_status);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}, */
	save:function(url){
		$('#materialscrapEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#materialscrapEditDialog').dialog('close');
                    });
                }else{
                    $('#materialscrapEditDialog').dialog('close');      
                    $('#materialscrapDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#materialscrapDatagrid').datagrid({ queryParams:{
				goods_cate:1000,
				page:1
			}});
	},
	reloadBar:function(){
		$('#materialscrapDatagrid').datagrid('reload');
	},
	pushBar:function(){
		var materialscrapRow=$('#materialscrapDatagrid').datagrid('getSelections');
		var text='确定推送吗！';
        if (materialscrapRow.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<materialscrapRow.length;i++){
                		ids.push(materialscrapRow[i].id);
						// if(materialscrapRow[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+materialscrapRow[i].materialscrap_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(materialscrappushUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}

}